using System.IO;
using System.Text;

namespace Core
{
    public class BuilderMicro
    {
        private Builder[] m_Builders;
        private string[] m_Templates;
        public BuilderMicro(string[] templates, Builder[] builders)
        {
            this.m_Builders = builders;
            this.m_Templates = templates;
        }
        public string Build()
        {
            var sb = new StringBuilder();
            foreach (Builder builder in this.m_Builders)
            {
                foreach (string t in this.m_Templates)
                {
                    sb.Append(builder.GetTempateValue(t));
                }
            }
            return sb.ToString();
        }

        public void Build(string path)
        {
            foreach (Builder builder in this.m_Builders)
            {
                foreach (string t in this.m_Templates)
                {
                    string fileName = Path.GetFileNameWithoutExtension(t);
                    string destDirectory = Path.Combine(path, fileName);
                    string destFile = Path.Combine(destDirectory, string.Concat(builder.Table.Table.Name, fileName, ".cs"));
                    if(!Directory.Exists(destDirectory))
                    {
                        Directory.CreateDirectory(destDirectory);
                    }
                    var sr = new StreamWriter(
                        destFile
                        ,false);
                    sr.Write(builder.GetTempateValue(t));
                    sr.Close();
                }
            }
        }
    }
}